package com.kairos.commands
{
	import com.bourre.commands.MockCommand;
	import com.bourre.transitions.MockBeacon;
	import com.kairos.engine.MockRTBeacon;
	
	import flexunit.framework.TestCase;
	import flash.events.Event;
	import com.bourre.commands.MockASyncCommandListener;

	public class IntervalRTTest extends TestCase
	{
		private var _oI : IntervalRT;
		private var _oB : MockRTBeacon;
		private var _oC : MockCommand;
		
		public override function setUp():void
		{
			_oB = MockRTBeacon.getInstance();
			_oC = new MockCommand ();
			_oI = new IntervalRT ( _oC, 100 );
			_oB.setTickBeacon( MockBeacon.getInstance() );
			_oB.setStep(50);
		}
		
		public override function tearDown() : void
		{
			_oB.removeTickListener( _oI );
			_oC = null;
			_oI = null;
			
			MockRTBeacon.release();
		}
		
		public function testConstruct () : void
		{
			assertNotNull ( "IntervalRT constructor returns null" , _oI );
			assertNotNull ( "IntervalRT contructor failed to register command", _oI.getCommand() );
			assertEquals ( "IntervalRT contructor failed to register the delay", 100, _oI.getDelay() );
		}
		
		public function testRun () : void
		{
			var e : Event = new Event ( "pouet" );
			
			_oB.addTickListener( _oI );
			_oI.setArgument( e );
			
			for(var i:int=0;i<9;i++) _oB.onTick( null );
			
			assertTrue ( _oI + " failed to execute the command at the end of the time", _oC.called );
			assertEquals ( _oI + " haven't call the command the good number of times." , 4, _oC.callCount );
			assertEquals ( _oI + " haven't passed the argument to the command", e, _oC.event );
		}
		
		public function testRunWithLocalSpeed () : void
		{
			var e : Event = new Event ( "pouet" );
			
			_oB.addTickListener( _oI );
			_oI.setLocalSpeed( 2 );
			_oI.setArgument( e );
			
			for(var i:int=0;i<9;i++) _oB.onTick( null );
			
			assertTrue ( _oI + " failed to execute the command at the end of the time", _oC.called );
			assertEquals ( _oI + " haven't call the command the good number of times." , 9, _oC.callCount );
			assertEquals ( _oI + " haven't passed the argument to the command", e, _oC.event );
		}
		
		public function testGetterSetter() : void
		{
			var o : MockCommand = new MockCommand();
			var e : Event = new Event ( "pouet" );
			
			_oI.setCommand( o );
			_oI.setArgument( e );
			_oI.setDelay( 15 );
			
			assertEquals ( _oI + ".getDelay() failed to return the last inserted value", 15, _oI.getDelay() );
			assertEquals ( _oI + ".getCommand() failed to return the last inserted value", o, _oI.getCommand() );
			assertEquals ( _oI + ".getArgument() failed to return the last inserted value", e, _oI.getArgument() );		
		}
		
		public function testStartStop() : void
		{
			_oI.start();
			
			assertTrue ( _oI + ".isRunning() don't return true after a start call", _oI.isRunning() );
			
			_oI.stop();
			
			assertFalse ( _oI + ".isRunning() don't return false after a stop call", _oI.isRunning() );
		}
		
		public function testIntervalCount() : void
		{			
			_oB.addTickListener( _oI );
			_oI.start();
			_oI.setCount( 6 );
			
			while( _oI.isRunning() ) _oB.onTick( null );
			
			assertTrue ( _oI + " failed to execute the command at the end of the time", _oC.called );
			assertEquals ( _oI + " haven't call the command the good number of times." , 6, _oC.callCount );
			assertFalse ( _oI + ".isRunning() don't return false after the count", _oI.isRunning() );
		}
		public function testASyncBehavior () : void
		{
			var o : MockASyncCommandListener = new MockASyncCommandListener();
			_oI.setCount( 4 );
			_oI.addASyncCommandListener( o );			
			_oB.addTickListener( _oI );
			_oI.start();
			
			while( _oI.isRunning() ) _oB.onTick( null );
			
			assertTrue ( _oI + " failed to execute the command end event at the end of the time", o.called );
		}		
		public function testStartAlias () : void
		{
			_oI.execute ();
			assertTrue( _oI + ".isRunning() return false after a call of execute", _oI.isRunning() );
			_oI.stop ();
			
			_oI.play ();
			assertTrue( _oI + ".isRunning() return false after a call of play", _oI.isRunning() );
			_oI.stop ();
		}
		public function testLocalSpeedGetterSetter () : void 
		{
			var errorOccurs : Boolean;
			
			errorOccurs = false;
			try
			{
				_oI.setLocalSpeed( 0 );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			
			assertTrue( _oI + ".setLocalSpeed() don't failed with a value of 0", errorOccurs );
			assertEquals ( _oI + ".setLocalSpeed() with an invalid argument have modify the local speed", 1, _oI.getLocalSpeed() );
			
			errorOccurs = false;
			try
			{
				_oI.setLocalSpeed( -1 );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			
			assertTrue( _oI + ".setLocalSpeed() don't failed with a negative value", errorOccurs );
			assertEquals ( _oI + ".setLocalSpeed() with an invalid argument have modify the local speed", 1, _oI.getLocalSpeed() );
			
			
			_oI.setLocalSpeed( 2 );
			assertEquals ( _oI + ".setLocalSpeed() failed to change the local speed", 2, _oI.getLocalSpeed() );
			
		}
		public function testTickBeaconGetterSetter () : void 
		{
			var errorOccurs : Boolean;
			
			errorOccurs = false;
			try
			{
				_oI.setTickBeacon( null );
			}
			catch ( e : Error )
			{
				errorOccurs = true;
			}
			
			assertTrue( _oI + ".setTickBeacon() don't failed with a null beacon", errorOccurs );
			
			var beacon : MockRTBeacon = new MockRTBeacon ();
			
			_oI.setTickBeacon( beacon );
			
			assertEquals ( _oI + ".setTickBeacon() failed to set the new beacon", beacon, _oI.getTickBeacon() );
		}
	}
}